local skel = fk.CreateSkill{
  name = "lb__hunbing",
}

Fk:loadTranslationTable{
  ["lb__hunbing"] = "魂兵",
  --[":lb__hunbing"] = "你可将场上一张武器牌当无视距离的【杀】对其持有者使用，若其因此受到伤害，其使用之，否则你获得之。",

  --[":lb__hunbing"] = "每回合限一次，你可将场上一张武器牌当无视距离的【杀】对其持有者使用，若其因此受到伤害，其使用之，否则你获得之。",
  --9.9
  [":lb__hunbing"] = "每回合限一次，你可将场上一张武器牌当无视距离且不计次数的【杀】对其持有者使用，若其因此受到伤害，其使用之，否则你获得之。",

  ["#lb__hunbing"] = "魂兵：你可将场上一张武器牌当无视距离且不计次数的【杀】对其持有者使用，若其因此受到伤害，其使用之，否则你获得之",
  ["#lb__hunbing_ask"] = "魂兵：将一张武器牌当【杀】对 %dest 使用，若其因此受到伤害，其使用之，否则你获得之",

  ["$lb__hunbing1"] = "我要将你们，尽数熔断！",
  ["$lb__hunbing2"] = "魔剑尚未敛尽。",
}


skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#lb__hunbing",
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".",
  },
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    return nil
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected == 0 and table.find(to_select:getCardIds("ej"), function (id)
        local weapon = Fk:getCardById(id)
        local slash = Fk:cloneCard("slash")
        slash.skillName = skel.name
        slash:addSubcard(id)
        return weapon.sub_type == Card.SubtypeWeapon and player:canUseTo(slash, to_select, {bypass_distances = true, bypass_times = true})
      end)
  end,
  on_use = function(self, room, cardUseEvent, _, params)
    local player = cardUseEvent.from
    local to = cardUseEvent.tos[1]
    local card = Fk:cloneCard("slash")
    card.skillName = skel.name
    local ids = table.filter(to:getCardIds("ej"), function (id)
      local weapon = Fk:getCardById(id)
      local slash = Fk:cloneCard("slash")
      slash.skillName = skel.name
      slash:addSubcard(id)
      return weapon.sub_type == Card.SubtypeWeapon and player:canUseTo(slash, to, {bypass_distances = true, bypass_times = true} )
    end)
    if #ids > 0 then
      if #ids > 1 then
        ids[1] = room:askToChooseCard(player, {
          target = to,
          flag = { card_data = { { "weapon", ids } } },
          skill_name = skel.name,
          prompt = "#lb__hunbing_ask::"..to.id,
        })
      end
      card:addSubcard(ids[1])
      return {  ---@type UseCardDataSpec
        from = player,
        tos = {to},
        card = card,
        extra_data = { lb__hunbing = {player = to.id, card = ids[1]} },
        extraUse = true, --9.9
      }
    end
  end,
  feasible = function(self, player, selected, selected_cards, card)
    return #selected == 1
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(skel.name) == 0
  end,
  enabled_at_response = Util.FalseFunc,
})

skel:addEffect(fk.CardUseFinished, {
  can_refresh = function(self, event, target, player, data)
    if target == player and data.extra_data and data.extra_data.lb__hunbing then
      local id = table.find(data.card.subcards, function (id)
        return data.extra_data.lb__hunbing.card == id
      end)
      return id and player.room:getCardArea(id) == Card.Processing
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:getCardById(data.extra_data.lb__hunbing.card)
    if #room.logic:getActualDamageEvents(1, function (e)
      local damage = e.data
      return damage.to.id == data.extra_data.lb__hunbing.player and not damage.prevented
    end, nil, room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true).id) > 0 then
      local to = room:getPlayerById(data.extra_data.lb__hunbing.player)
      room:useCard{
        from = to,
        tos = {to},
        card = card,
      }
    else
      room:obtainCard(player, card, true, fk.ReasonJustMove, player, skel.name)
    end
  end,
})

return skel